<?php if(!defined('ARCH_ROOT_PATH')) exit;
/**
 *	Architect Framework
 *
 *	Architect Framework is a light weight object oriented web applications framework written for PHP 5.1.4 and later that provide the tools to develop and deploy a website or application fast and with as little code as possible.
 *
 *	@author Robin Grass, <hello@coffinboy.se>
 *	@link http://architect.kodlabbet.net
 *	@license http://www.opensource.org/licenses/gpl-3.0.html GNU General Public License (v. 3)
 */

/**
 *	Architect_Config
 *
 *	This class parses through config.xml.
 *
 *	@package Core
 *	@version 1.0
 *	@author Robin Grass, <hello@coffinboy.se>
 */
class Architect_Config {

	/**
	 *	getConfigXML
	 *
	 *	This method returns a new DOMXPath object of the config.xml file.
	 *
	 *	@return object
	 */
	protected function getConfigXMLAsXPath() {
		return new DOMXPath(DOMDocument::load(ARCH_ROOT_PATH . 'config.xml'));
	}
	
	/**
	 *	getRouteMaps
	 *
	 *	This method parses through the route maps in the config file and returns a standardized associatibe array for Architect_Router.
	 *
	 *	@return array
	 */
	public function getRouteMaps() {
		$routes = array();
		$xpath = $this->getConfigXMLAsXPath();
		
		// Set default controller
		$routes['default_controller'] = $xpath->query('//routes/default')->item(0)->nodeValue;
		
		// Iterate through each route-element
		foreach($xpath->query('//route') as $index => $route) {
			
			// Set route variables
			$pattern = $route->getAttribute('path');
			$controller = $xpath->query('//route[' . ($index + 1) . ']/controller', $route)->item(0)->nodeValue;
			$method = $xpath->query('//route[' . ($index + 1) . ']/controller[@method]')->item(0);
			$params = array();
			
			// Is there a method attribute in this route element?
			if(is_object($method)) {
				$method = $method->getAttribute('method');
			} else $method = null;
			
			// Get all the param elements for this route
			$_params = $xpath->query('//route[' . ($index + 1) . ']//param', $route);
			foreach($_params as $param)
				$params[] = $param->nodeValue;
			$params = trim(implode('/', $params), '/');
			
			// Assign route pattern to it's class/method/params - array('my/route' => 'class/method/params')
			$routes[$pattern] = trim(implode('/', array($controller, $method, $params)), '/');
		}
		
		// Sort patterns by key and reverse the array
		ksort($routes);
		$routes = array_reverse($routes);
		
	return $routes;
	}
	
	/**
	 *	getVariable
	 *
	 *	This method returns the value (or false) of a element found in the vars or appmeta element in the config file.
	 *
	 *	@param string $element
	 *	@param string $variable
	 *	@param string $attribute Optional.
	 *
	 *	@return string|bool
	 */
	protected function getVariable($element, $variable, $attribute = null) {
		$xpath = $this->getConfigXMLAsXPath();
		
		// Get the child element of either the vars or appmeta elements.
		$element = $xpath->query("//{$element}/{$variable}")->item(0);
		
		// Nope, element doesn't exist, return false.
		if(!$element)
			return false;
		
		// Are we looking for a attribute value from this element?
		if($attribute) {
			// Yeah, and the attribute exists too!
			$attribute_value = $element->getAttribute($attribute);
			if($attribute_value)
				return $attribute_value;
			// Attribute doesn't exist, return element value
			else return $element->nodeValue;
		}
	// Unless we want the attribute (see above)
	return $element->nodeValue;
	}
	
	
	/**
	 *	getVar
	 *
	 *	This method returns the value (or false) of a element found in the vars element in the config file.
	 *
	 *	@return string|bool
	 */
	public function getVar($variable, $attribute = null) {
		return $this->getVariable('vars', $variable, $attribute);
	}
	
	/**
	 *	getMetaVar
	 *
	 *	This method does the same as Architect_Config::getVar but for elements in the appmeta element.
	 *
	 *	@return string|bool
	 */
	public function getMetaVar($variable, $attribute = null) {
		return $this->getVariable('appmeta', $variable, $attribute);
	}

}
?>